package com.naiterui.ehp.bs.cms.modules.agent.service.impl;

import java.util.Date;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.notice.AppNoticeUtil;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.agent.entity.AgNoticeMessage;
import com.naiterui.ehp.bs.cms.modules.agent.mapper.AgNoticeMessageMapper;
import com.naiterui.ehp.bs.cms.modules.agent.po.NoticeEditPO;
import com.naiterui.ehp.bs.cms.modules.agent.po.NoticePagePO;
import com.naiterui.ehp.bs.cms.modules.agent.service.IAgentNoticeService;
import com.naiterui.ehp.bs.cms.modules.agent.vo.NoticeItemVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.NoticePageItemVO;

/**
 * @author yiren
 * @since 2022/2/16
 */
@Service
public class AgentNoticeServiceImpl extends ServiceImpl<AgNoticeMessageMapper, AgNoticeMessage> implements IAgentNoticeService {

	@Override
	public PageVO<NoticePageItemVO> queryPage(PageParamsVO pageParamsVO, NoticePagePO pagePO){
		QueryWrapper<NoticePagePO> entityWrapper = this.buildEntityWrapper(pageParamsVO, pagePO);

		IPage<NoticePageItemVO> page = this.baseMapper.getPage(pageParamsVO.getPage(), entityWrapper);

		return PageVO.toPageVo(page);
	}

	@Override
	public NoticeItemVO getDetail(Long id) {
		AgNoticeMessage message = this.getById(id);
		return NoticeItemVO.of(message);
	}

	@Override
	public void saveNotice(NoticeEditPO editPO, String userName) {
		Date thisTime = new Date();
		AgNoticeMessage message = Optional.ofNullable(editPO.getId()).map(this::getById).orElseGet(() -> {
			AgNoticeMessage item = new AgNoticeMessage();
			item.setDelFlag(AgNoticeMessage.DEL_FLAG_NO);
			item.setStatus(AgNoticeMessage.STATUS_BETA);
			item.setType(AgNoticeMessage.TYPE_NOTICE_SYS);
			return item;
		});
		message.setTitle(editPO.getTitle());
		message.setContent(editPO.getContent());
		message.setChangedAt(thisTime);
		if (editPO.getPushState() != null && editPO.getPushState() == 1){
			message.setReleaseBy(userName);
			message.setReleaseTime(thisTime);
			message.setStatus(AgNoticeMessage.STATUS_RELEASE);
			sendMessage(message);
		}
		this.saveOrUpdate(message);
	}

	@Override
	public void updateNoticeStatus(Long id, Integer status, String userName) {
		AgNoticeMessage message = this.getById(id);
		if (status == AgNoticeMessage.STATUS_RELEASE){
			message.setStatus(status);
			message.setReleaseTime(new Date());
			message.setReleaseBy(userName);
			sendMessage(message);
		} else {
			message.setStatus(status);
		}
		this.updateById(message);
	}

	private void sendMessage(AgNoticeMessage message){
		try {
			AppNoticeUtil.sendBatchNotice(ConfigUtil.getString("ehp.domain.agentPush"), AppNoticeUtil.NoticeType.AG_SYS_NOTICE,
					new AppNoticeUtil.SimpleNotice(message.getTitle(), message.getContent(), message.getReleaseTime().getTime()));
		}catch (Exception e){
			log.error("认证消息发送失败", e);
		}
	}

	private QueryWrapper<NoticePagePO> buildEntityWrapper(PageParamsVO pageParamsVO, NoticePagePO pagePO) {

		QueryWrapper<NoticePagePO> entityWrapper = pageParamsVO.getEntityWrapper("t");
		entityWrapper.eq("t.type", AgNoticeMessage.TYPE_NOTICE_SYS);
		entityWrapper.like(StringUtils.isNotEmpty(pagePO.getChangedBy()), "t.changed_by", pagePO.getChangedBy());
		entityWrapper.like(StringUtils.isNotEmpty(pagePO.getReleaseBy()), "t.release_by", pagePO.getReleaseBy());
		entityWrapper.eq(pagePO.getStatus() != null, "t.status", pagePO.getStatus());
		String startTime = pagePO.getReleaseTimeGte();
		String endTime = pagePO.getReleaseTimeLte();
		if (StringUtils.isNotEmpty(endTime)){
			endTime += " "+ DateUtil.MAX_TIME_IN_DAY;
		}

		entityWrapper.between(!StringUtils.isAnyEmpty(startTime,endTime), "t.release_time", startTime, endTime);

		startTime = pagePO.getChangedAtGte();
		endTime = pagePO.getChangedAtLte();
		if (StringUtils.isNotEmpty(endTime)){
			endTime += " "+ DateUtil.MAX_TIME_IN_DAY;
		}
		entityWrapper.between(!StringUtils.isAnyEmpty(startTime,endTime), "t.changed_at", startTime, endTime);
        entityWrapper.orderByDesc("t.changed_at");
		return entityWrapper;
	}
}
